﻿using System;
using agsXMPP;

namespace GmailChatBot
{
    class Program
    {
        static XmppClientConnection client;
        //static bool TvControllerStart = false;

        static void Main(string[] args)
        {
            try
            {
                client = new XmppClientConnection("gmail.com");
                client.AutoResolveConnectServer = false;
                client.ConnectServer = "talk.google.com";
                client.Open("nlareu.boot", "nicolaslareu1");

                client.OnLogin += delegate(object o)
                {
                    Console.WriteLine("Logged");
                };
                client.OnMessage += new agsXMPP.protocol.client.MessageHandler(xmpp_OnMessage);

                Console.ReadLine();
                client.Close();
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.Message);
            }
        }

        static void xmpp_OnMessage(object sender, agsXMPP.protocol.client.Message msg)
        {
            if (msg.Chatstate == agsXMPP.protocol.extensions.chatstates.Chatstate.composing)
                return;

            // This is called for any message received.
            Console.WriteLine("Received: " + msg.Body);

            // Let our chat bot respond to the message.
            if (msg.Body == null || msg.Body == "")
                return;

            string response;
            string msgFixed = msg.Body.Trim();
            int blankCharIndex = msgFixed.IndexOf(" ");
            string comHeader = "";
            string comBody = "";
            CommandProcess cmdProc;

            if (blankCharIndex == -1)
            {
                comHeader = msgFixed.ToUpper();
            }
            else
            {
                comHeader = msgFixed.Substring(0, blankCharIndex).ToUpper();
                comBody = msgFixed.Substring(blankCharIndex + 1, msgFixed.Length - blankCharIndex - 1);
            }

            switch (comHeader)
            {
                //case "TEST01":
                //    response = "Testeo 1";
                //    break;
                //case "TEST02":
                //    response = "Testing 2";
                //    break;
                case "PC":
                    cmdProc = new KeyCommadProcess();
                    break;
                //case "TV":
                //    response = Program.ProcessTvCommand(comBody);
                //    break;
                default:
                    cmdProc = new CommandProcess();
                    break;
            }

            response = cmdProc.Process(comBody);

            // Create a new message.
            agsXMPP.protocol.client.Message newmsg = new agsXMPP.protocol.client.Message();
            newmsg.Type = agsXMPP.protocol.client.MessageType.chat;
            newmsg.To = new Jid(msg.From.Bare);
            newmsg.Body = response;

            // Send response.
            client.Send(newmsg);
        }
    }
}
